﻿/*
 * Created by SharpDevelop.
 * User: André Casaca
 * Date: 02-07-2011
 * Time: 17:59
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Data;
using Mono.Data.SqliteClient;
using System.Text;

namespace LP_C__5847_2010_2011
{
	/// <summary>
	///Write All Statistics On a File.txt.
	/// </summary>
	public class Write
	{
		/// <summary>
     	/// The constructor sets the year of the file adn the sortType
     	/// </summary>
     	/// <param name="year">Statistics Year to Write</param>
     	/// <param name="sortType">What type of sort</param>
		public Write(int year, int sortType)
		{
			string s = "";
			string fileName = "";
			do{
			Console.Write("\n Name of the file you want to write: ");
			Console.ForegroundColor = ConsoleColor.Blue;						
      		fileName = Console.ReadLine(); 
      		Console.ForegroundColor = ConsoleColor.White;
      		
      		fileName = fileName + (year+2000) + ".txt";
			
			if (System.IO.File.Exists(fileName))
        	{
				Console.Write("\n Wana overwright the existing file {0} ? [yes, no] : ", fileName);
				Console.ForegroundColor = ConsoleColor.Blue;						
      			s = Console.ReadLine(); 
      			Console.ForegroundColor = ConsoleColor.White;
      			if(s.Equals("yes")){
      				System.IO.File.Delete(fileName);
      				Console.Write("\n File deleted with succsses...\n");
      			}	
        	}
			}while(System.IO.File.Exists(fileName));
			
			WriteOnText(fileName, year, sortType);
			Console.Write("\n DONE!");
		}
		
		/// <summary>
     	/// Write on the text sorting and creating the file
     	/// </summary>
     	/// <param name="fileName">File's Name</param>
     	/// <param name="year">Statistics's Year to Write</param>
     	/// <param name="sortType">What type of sort</param>
		public static void WriteOnText(string fileName, int year, int sortType){
			Console.ForegroundColor = ConsoleColor.Blue;
			using (System.IO.FileStream fs = System.IO.File.Create(fileName, 1024)){
				//1
            	writeLine(fs, "\n Year : " +  (year+2000) + " ->  " + Statistics.GetNumTeachers(year)+ " teachers" + Environment.NewLine);
            	Console.Write("\n 7%");
      
            	//2
            	Dictionary<string, int> establishments = Statistics.NumberTeachersEstablishmentYear(year, "estabelecimentos");
            	establishments = Program.SortType(establishments, sortType);
            	WriteDictionaryValueInt(fs, establishments);
            	Console.Write("\n 14%");
			
            	//3
           	 	Dictionary<string, int> degrees = Statistics.NumberTeachersEstablishmentYear(year, "graus");
           	 	degrees =  Program.SortType(degrees, sortType);
            	WriteDictionaryValueInt(fs, degrees);
            	Console.Write("\n 21%");
            
            	//4
            	Dictionary<string, int> degreesAndEstablishments = Statistics.NumberTeachersEstablishmentYear(year, "graus,estabelecimentos"); 
            	degreesAndEstablishments = Program.SortType(degreesAndEstablishments, sortType);
            	WriteDictionaryValueInt(fs, degreesAndEstablishments);
            	Console.Write("\n 28%");
            	
            	//5
            	Dictionary<string, int> NumTeachersDoct = Statistics.NumTeachersDoctorYear(year);
            	NumTeachersDoct = Program.SortType(NumTeachersDoct, sortType);
            	WriteDictionaryValueInt(fs, NumTeachersDoct);
            	Console.Write("\n 35%");
            	
            	//6
            	Dictionary<string, List<Person>> listTeachersDoct = Statistics.ListTeachersDoctorYear(year);
            	WriteDictionaryValueList(fs, listTeachersDoct);
            	Console.Write("\n 43%");
            	
            	//7
            	Statistics.ListMigratersTeachers(year);	
				Console.Write("\n 47%");            	
         		
				//8            	
            	List<string> listEstablishments = Statistics.PrintListEstablishments(year);       				
            	listEstablishments.Sort();
				foreach (string k in listEstablishments){
					writeLine(fs, k + Environment.NewLine);
				}
            	Console.Write("\n 53%");
            	
         		//9            
           		List<Person> listTeachers = Statistics.GetListTeachers(year);
           		List<string> listNameTeachers = new List<string>();
           		foreach (Person k in listTeachers){
           			if(!listNameTeachers.Contains(k.ToStr)){
           				listNameTeachers.Add(k.ToStr);
           			}				
				}
           		listNameTeachers.Sort();
				foreach (string k in listNameTeachers){
					writeLine(fs, k + Environment.NewLine);
				}
           		writeLine(fs, Environment.NewLine);
           		Console.Write("\n 64%");
				
           		//10
				Dictionary<string, List<Person>> listLeavers = Statistics.PrintLeaversNewersList(year, 10);
				WriteDictionaryValueList(fs, listLeavers);
				Console.Write("\n 72%");				
         		
				//11
            	Dictionary<string, List<Person>> listNewers = Statistics.PrintLeaversNewersList(year, 11);  
				WriteDictionaryValueList(fs, listNewers);
				Console.Write("\n 79%");				
         		
				//12
							//dont need sort							
				int numMigraters = Statistics.NumberMigratersTeachers(year);
				writeLine(fs, "" + numMigraters + Environment.NewLine + Environment.NewLine);
				Console.Write("\n 85%");
				
				//13
				Dictionary<string, int> teacherPromList = Statistics.NumberPromotedTeachers(year);	
				teacherPromList =  Program.SortType(teacherPromList, sortType);
            	WriteDictionaryValueInt(fs, teacherPromList);	
				Console.Write("\n 100%");            	
        	}
		}
     	
		/// <summary>
     	/// Write a dictionary with a values type int
     	/// </summary>
     	/// <param name="fs">file's Stream</param>
     	/// <param name="dict">dictionary to write</param>
		public static void WriteDictionaryValueInt(System.IO.FileStream fs, Dictionary<string, int> dict){
			foreach (KeyValuePair<string, int> entry in dict){
					writeLine(fs, "" + entry.Key + ": " +  entry.Value + Environment.NewLine);
				}
            writeLine(fs, Environment.NewLine);
		}
		
     	/// <summary>
     	/// Write a dictionary with a values type List
     	/// </summary>
     	/// <param name="fs">file's Stream</param>
     	/// <param name="dict">dictionary to write</param>
		public static void WriteDictionaryValueList(System.IO.FileStream fs, Dictionary<string, List<Person>> dict){
			foreach (KeyValuePair<string, List<Person>> entry in dict){
            	writeLine(fs, entry.Key + ": " + Environment.NewLine);
				foreach (Person k in entry.Value){
					writeLine(fs, k.ToStr + Environment.NewLine);
				}
				writeLine(fs, Environment.NewLine);
		}
		}
		
     	/// <summary>
     	/// Write a Line on text
     	/// </summary>
     	/// <param name="fs">file's Stream</param>
     	/// <param name="toWrite">The Line of text to write on file</param>
		public static void writeLine(System.IO.FileStream fs, string toWrite){
			byte[] text = new System.Text.UTF8Encoding(true).GetBytes(toWrite);
            fs.Write(text, 0, text.Length);	
		}
	}
}
